<?php

namespace App\Http\Controllers\VipChannel;

use App\Modules\Channel\Models\ChannelUser;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\User\Services\VipManageService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class BaseController extends Controller
{
    //登录用户ID
    protected $user_id;
    //登录用户类型： manage @vip管理员，channel_user @分销账户
    protected $user_type;
    //获取用户信息
    protected $user_info;

    public function setInfo(Request $request){
        $this->user_id = session('uid');
        $this->user_type = session('user_type');
        $this->user_info = session('user_info');

        if (!$this->user_id || !$this->user_type) {
            return response()->error('NOT_LOGIN');
        }
    }

    public function getManageType(Request $request)
    {
        if(!$this->user_type){
            $this->setInfo($request);
        }
        return $this->user_type;
    }

    //获取登陆管理员id
    function getManagerId(Request $request)
    {
        if(!$this->user_id) {
            $this->setInfo($request);
        }
        return $this->user_id;
    }

    //获取登陆管理员下权限账号
    function getChannelUsers(Request $request)
    {
        // $manage = $this->getManageUserInfo();
        $this->setInfo($request);
        if ($this->user_type == 'manage') {
            $manage = VipManageService::getChannelusers($this->user_id);
            $res = [];
            foreach ($manage as $item) {
                $res[] = $item->channel_user_id;
            }
            return $res;
        }

        if ($this->user_type == 'channel_user') {
            $channel_users = ChannelUserService::getById($this->user_id);
            if ($channel_users) {
                return [$channel_users->id];
            }
        }
        return [];
    }

    function getManageUserInfo(Request $request)
    {
        if(!$this->user_info){
            $this->setInfo($request);
        }
        return unserialize($this->user_info);
    }

    function getDistributionChannelIds(Request $request)
    {
        $channels = ChannelUser::getChannelIdsByChannelUserIds($this->getChannelUsers($request));
        $res = [];
        foreach ($channels as $channel) {
            $res[] = $channel->id;
        }
        return $res;
    }
}
